var util = require('../../../utils/util.js');
var app = getApp();

Page({
  data: {
    txt2: {
    },
    dialog4: {
      hidden: true
    },
    dialog5: {
      hidden: true
    },
    name: "小区位置",
    ysex: ['男', '女'],
    init:0,
    formSubmit: 'submit',
    yfilename: "",
    yname:"",
    yphones:"",
    unitdoor:"",
    yidcar:"",
    imgUrls: [
      '../../../image/upload.jpg',
    ]
   
  },
  onLoad: function (e) {
       /*wx.request({
         url: 'https://wwl.ycws.cc/ssm/visitors/openid',
         data: {
           openid: app.globalData.openId
         },
         header: {
           'content-type': 'application/json'
         },
         success: function (res) {
           console.log(res.data)
           if (res.data != null) {
             console.log("已经注册");
             var an = res.data;
             var json = JSON.stringify(an);
             app.owner.json = json;
             wx.redirectTo({
               url: '../../../pages/Visitor/showuser/showuser'
             })
           }
         }
       })*/
    //页面传值过来进行处理
    var that = this;
    that.setData({ formSubmit: "submit" });
    that.setData({yphones:e.phone});
    if (app.address != ""){
    var a = app.yfilename;
    var b = app.yname;
    var c = app.yphones;
    var d = app.ysex;
    var e = app.address;
    var f = app.unitdoor;
    var yidcar = app.yidcar;
    that.setData({ yfilename: a });
    that.setData({ yname: b });
    that.setData({ yphones: c });
    that.setData({ init: d });
    that.setData({ name: e });
    that.setData({ unitdoor: f });
    that.setData({ yidcar: yidcar});
    }
  },
  //性别
  bindPickerChange: function (e) {
    this.setData({
      init: e.detail.value
    })
    console.log("ysexinit" + this.data.init);
  },
  //业主
  bindPickerChange1: function (e) {
    this.setData({
      yownerinit: e.detail.value
    })
    console.log("yownerinit" + this.data.yownerinit);
  },
  //单元门
  unitChange: function (e) {
    this.setData({
      unitinit: e.detail.value
    })
    console.log("unitinit" + this.data.unitinit);
  },
  //图片选择1
  uploads: function () {
    var that = this;
    wx.chooseImage({  
      count: 1, // 默认9
      sizeType: ['compressed'],
      //sizeType: ['original', 'compressed'],   // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'],          // 可以指定来源是相册还是相机，默认二者都有
      success: function (res1) {
        wx.uploadFile({
          url: util.getBasePath() + "/ccCustomerApiController/uploadPhoto",
          filePath: res1.tempFilePaths[0],
          name: 'file',
          success: function (res) {
            debugger
            var s = JSON.parse(res.data);
            if (s.retCode == "500") {
              util.msg("提示", "不是人脸图片，请重新上传");
              that.setData({ 'imageHidden.hidden': false });
              that.setData({ 'dialog4.hidden': true });
            } else if (s.retCode == "200") {
              var retCode = s.retCode;
              var fkurl = s.retDetail.photoFileId;
              wx.setStorageSync('fkurl', fkurl);
              console.log("是人脸图片");
              that.setData({ 'dialog4.hidden': true })
              that.setData({
                yfilename: res1.tempFilePaths[0],
              })
            }
          }
        })
      }
    })
  },
  dengdai:function(e){
    util.msg("提示", "信息上传中,请稍等...");
  },
  primary: function (e) {
    
    var that = this
    var yfilename = this.data.yfilename;
    var yaddresss = that.data.name;
    var ynames = e.detail.value["yname"]
    var yphones = e.detail.value["yphones"]
    var unitdoor = e.detail.value["unitdoor"]
    var ysexs = this.data.init;
    var name = /(?!(\D*\d){11})^[\u4e00-\u9fa5\w]{2,21}$/;
    if (ysexs == 0) {
      var ysexs = "男";
    } else if (ysexs == 1) {
      var ysexs = "女";
    }
    if (yfilename == "") {
      util.msg("提示","请上传图片");
    } else if (ynames == "") {
      util.msg("提示", "请输入姓名");
    } else if (!name.test(ynames)){
      util.msg("提示", "请输入2-12位中文或者字母或者数字，总数不能超过21位（数字的个数不能超过11个,不能有空白格）");
    }else if (yphones == "") {
      util.msg("提示", "请输入手机号码");
    } else if (!util.validatePhone(yphones)) {
      util.msg("提示", "手机号码不符");
    } else if (yaddresss == '小区位置') {
      util.msg("提示", "请选择小区地址");
    } else if (unitdoor == ""){
      util.msg("提示", "请输入单元楼");
    } else{
      //that.setData({ formSubmit: "none" });
      that.setData({ 'dialog5.hidden': false });
      wx.uploadFile({
        url: 'https://wwl.ycws.cc/ssm/visitors/insert', 
        //url: 'http://localhost:8080/ssm/visitors/insert', 
        filePath: yfilename,
        name: 'file',
        formData: {
          'yname': ynames,
          'yphone': yphones,
          'ysex': ysexs,
          'yaddress': yaddresss,
          'yunitdoor': unitdoor,
          'openid': wx.getStorageSync('developer'),
        },
        success: function (res) {
       
          var json = JSON.parse(res.data);
          console.log(json)
          if (json.code==1){
            console.log("ok");
           wx.request({
             //url: 'http://localhost:8080/ssm/visitors/openid', 
             url: 'https://wwl.ycws.cc/ssm/visitors/openid',
              data: {
                openid: wx.getStorageSync('developer')
              },
              method: "POST",
              header: { 'content-type': 'application/x-www-form-urlencoded' },
              success: function (res) {
                that.setData({ 'dialog5.hidden': true });
                var an = res.data;
                var json = JSON.stringify(an);
                app.owner.json = json;
                that.setData({ 'dialog4.hidden': true });
                //util.add("提示", "添加成功");
                wx.navigateTo({
                  url: '../../../pages/Visitor/showuser/showuser'
                })
              }
            })
          }else{
            util.msg("提示", "网络异常");
          }
        }
      })
    }
  },
  address: function (e) {
    var that = this;
    var a = this.data.yfilename;
    var b = e.detail.value["yname"];
    var c = e.detail.value["yphones"];
    var d = this.data.yownerinit;
    var e = this.data.init;
    wx.chooseLocation({
      type: 'wgs84',
      success: function (res) {
        var latitude = res.latitude
        var longitude = res.longitude
        var speed = res.speed
        var accuracy = res.accuracy
        var name = res.name
        var address=res.address


        that.setData({ 'dialog5.hidden': true });
        var data = { longitude: longitude, latitude: latitude }
        util.ajax("post", "/smallRoutineController/longlatitude", data, function (data) {
          var list = data.list.location
          console.log(list[0], list.length)
          wx.redirectTo({
            url: '../../../pages/Visitor/showaddress1/showaddress1?a=' + list + '&b=' + that.data.ynumber + '&aa=' + a + '&dd=' + d + '&ee=' + e + '&ff=' + f
          })
        })
        wx.redirectTo({
          url: '../../../pages/Visitor/showaddress1/showaddress1?a=' + json + '&aa=' + a + '&bb=' + b + '&cc=' + c + '&dd=' + d + '&ee=' + e
        })

        wx.request({
          url: 'https://wwl.ycws.cc/ssm/location/longlatitude',
          data: {
            longitude: longitude,
            latitude: latitude
          },
          header: {
            'content-type': 'application/json'
          },
          success: function (res) {
            console.log(res.data.location);
            if (res.data.location == undefined) {
              util.msg("提示","您选择的小区暂未开放，请重新获取");
            } else {
              var an = res.data;
              var json = JSON.stringify(an);
              console.log("an" + json);
              
              wx.redirectTo({
                url: '../../../pages/Visitor/showaddress1/showaddress1?a=' + json + '&aa=' + a + '&bb=' + b + '&cc=' + c + '&dd=' + d + '&ee=' + e
              })
              
            }
          }
        })
      }
    })
  }
})





